<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\TestExport;
use App\Imports\TestSourceImport;
use Illuminate\Support\Facades\Hash;
use Exception;
use Illuminate\Support\Facades\Session;


class AnalyseController extends Controller {


    /*
     * 销售数据分析
     * */
    public function sell_analyse(Request $request){
            //Redis::rpush('list1', 'ab1');
            //var_dump(Redis::lpop('list1'));

           if(Session::get('role_id') == 1 ) {
               $servicer = DB::table('supplyer_company')
               ->select('name')
               ->get();

           }else if(Session::get('role_id') == 2 ){
                $servicer = DB::table('supplyer_company')
               ->select('name')
               ->where('company_id',Session::get('session_id'))->get();
            }


           $servicer_id = [];
           foreach ($servicer as $key=>$value){
               $servicer_id[] = $value->name;
           }

           if(!isset($request->start_date)&&empty($request->start_date)&&!isset($request->end_date)&&empty($request->end_date)) {
               $order = DB::table('order')
                   ->select('order_product_id', 'company_name', 'driver_id', 'driver_name', 'driver_get_date', 'arriver_date', 'make_date', 'supplyer_company', 'total_sell')
                   ->where('driver_status', '>=', 1)
                   ->where('supplyer_company', 'like', '%' . $request->supplyer_company . '%')
                   ->whereIn('supplyer_company', $servicer_id)
                   ->get();
           }else{
               $order = DB::table('order')
                   ->select('order_product_id', 'company_name', 'driver_id', 'driver_name', 'driver_get_date', 'arriver_date', 'make_date', 'supplyer_company', 'total_sell')
                   ->where('driver_status', '>=', 1)
                   ->where('make_date','>',$request->start_date.'00:00:00')
                   ->where('make_date','<',$request->end_date.'00:00:00')
                   ->where('supplyer_company', 'like', '%' . $request->supplyer_company . '%')
                   ->whereIn('supplyer_company', $servicer_id)
                   ->get();
           }

           $order_product_id = [];
           $driver_info = [];
           $supplyer_company = [];
           foreach ($order as $key=>$value){
              $order_product_id[] = explode(',',$value->order_product_id);
              $arr = explode(',',$value->order_product_id);
              if(!in_array($value->supplyer_company,$supplyer_company))
              $supplyer_company[] = $value->supplyer_company;

              foreach ($arr as $k=>$v){
               $driver_info[$v]['driver_id'] = $value->driver_id;
               $driver_info[$v]['driver_name'] = $value->driver_name;
               $driver_info[$v]['driver_get_date'] = $value->driver_get_date;
               $driver_info[$v]['arriver_date'] = $value->arriver_date;
               $driver_info[$v]['supplyer_company'] = $value->supplyer_company;
               $driver_info[$v]['total_sell'] = $value->total_sell;
               $driver_info[$v]['make_date'] = $value->make_date;
              }
           }
           $arr_id = [];
           foreach ($order_product_id as $key=>$value){
             $arr_id = array_merge($arr_id,$value);
           }

           $order_product = DB::table('order_product')
               ->select('*')
               ->whereIn('id',$arr_id)->get();

           $order_product = json_decode($order_product,1);
           foreach ($order_product as $key=>$value){
              $order_product[$key]['driver_id'] = $driver_info[$value['id']]['driver_id'];
              $order_product[$key]['driver_name'] = $driver_info[$value['id']]['driver_name'];
              $order_product[$key]['driver_get_date'] = $driver_info[$value['id']]['driver_get_date'];
              $order_product[$key]['arriver_date'] = $driver_info[$value['id']]['arriver_date'];
              $order_product[$key]['supplyer_company'] = $driver_info[$value['id']]['supplyer_company'];
              $order_product[$key]['total_sell'] = $driver_info[$value['id']]['total_sell'];
              $order_product[$key]['make_date'] = $driver_info[$value['id']]['make_date'];
           }
            $order_product  = (object)$order_product;
           return view('admin/analyse/sell_analyse',['product'=>$order_product,'supplyer_company'=>$supplyer_company]);

    }






    /*
     * 公司数据分析
     * */
    public function company_analyse(Request $request){
            //Redis::rpush('list1', 'ab1');
            //var_dump(Redis::lpop('list1'));

           if(Session::get('role_id') == 1 ) {
               $servicer = DB::table('supplyer_company')
               ->select('name')
               ->get();

           }else if(Session::get('role_id') == 2 ){
                $servicer = DB::table('supplyer_company')
               ->select('name')
               ->where('company_id',Session::get('session_id'))->get();
            }


           $servicer_id = [];
           foreach ($servicer as $key=>$value){
               $servicer_id[] = $value->name;
           }

           $order = DB::table('order')
               ->select('order_product_id','driver_id','driver_name','driver_get_date','company_name','arriver_date','supplyer_company','total_sell')
               ->where('driver_status','>=',1)
               ->where('company_name','like','%'.$request->company_name.'%')
               ->whereIn('supplyer_company',$servicer_id)
               ->get();

           $order_product_id = [];
           $driver_info = [];
           $company_name = [];
           foreach ($order as $key=>$value){
              $order_product_id[] = explode(',',$value->order_product_id);
              $arr = explode(',',$value->order_product_id);
              if(!in_array($value->company_name,$company_name)){
                  $company_name[] =$value->company_name;
              }
              foreach ($arr as $k=>$v){
               $driver_info[$v]['driver_id'] = $value->driver_id;
               $driver_info[$v]['driver_name'] = $value->driver_name;
               $driver_info[$v]['driver_get_date'] = $value->driver_get_date;
               $driver_info[$v]['arriver_date'] = $value->arriver_date;
               $driver_info[$v]['supplyer_company'] = $value->supplyer_company;
               $driver_info[$v]['total_sell'] = $value->total_sell;
              }
           }
           $arr_id = [];
           foreach ($order_product_id as $key=>$value){
             $arr_id = array_merge($arr_id,$value);
           }

           $order_product = DB::table('order_product')
               ->select('*')
               ->whereIn('id',$arr_id)->get();

           $order_product = json_decode($order_product,1);
           foreach ($order_product as $key=>$value){
              $order_product[$key]['driver_id'] = $driver_info[$value['id']]['driver_id'];
              $order_product[$key]['driver_name'] = $driver_info[$value['id']]['driver_name'];
              $order_product[$key]['driver_get_date'] = $driver_info[$value['id']]['driver_get_date'];
              $order_product[$key]['arriver_date'] = $driver_info[$value['id']]['arriver_date'];
              $order_product[$key]['supplyer_company'] = $driver_info[$value['id']]['supplyer_company'];
              $order_product[$key]['total_sell'] = $driver_info[$value['id']]['total_sell'];
           }
            $order_product  = (object)$order_product;
           return view('admin/analyse/company_analyse',['product'=>$order_product,'company_name'=>$company_name]);
    }

    /*
     * driver_analyse
     * */

    public function driver_analyse(){

         if(Session::get('role_id') == 1 ) {
             $driver = DB::table('driver')
                 ->where('status',0)
                 ->select('id', 'name', 'face_url', 'telephone', 'nikename', 'car_photo','car_license','person_license')
                 ->get();
         }else if(Session::get('role_id') == 2 ) {
             $driver = DB::table('driver')
                 ->where('company_id',Session::get('session_id'))
                 ->where('status',0)
                 ->select('id', 'name', 'face_url', 'telephone', 'nikename', 'car_photo','car_license','person_license')
                 ->get();
         }

             $driver = json_decode($driver,1);
             foreach ($driver as $key=>$value){

                 $driver[$key]['face_url'] = '/face_url/'.$value['face_url'];
                 $driver[$key]['car_photo'] = '/image/'.$value['car_photo'];
                 $driver[$key]['car_license'] = '/image/'.$value['car_license'];
                 $driver[$key]['person_license'] = '/image/'.$value['person_license'];


             }

                return view('/admin/analyse/driver_analyse',['driver'=>$driver]);

    }


        public function export($id)
        {
            //dd(new TestExport($id));
            return view('/admin/analyse/base');
                //return Excel::download(  new TestExport($id),'234234.xlsx');
        }



       /*
     * 业务个人数据分析
     * */
    public function person_analyse(){
            $servicer_percentage = DB::table('order_servicer_percentage')->get();

            $servicer_percentage = json_decode($servicer_percentage,1);

            foreach ($servicer_percentage as $key=>$value){
                  $servicer = DB::table('servicer')
                      ->select('name')
                      ->where('id',$value['servicer_id'])
                      ->first();
                   if(!empty($servicer)){
                  $servicer_percentage[$key]['servicer_name'] = $servicer->name;
                  }else{
                  $servicer_percentage[$key]['servicer_name'] = '已被删除';
                  }
            }


            return view('admin/analyse/person_analyse',['servicer_percentage'=>$servicer_percentage]);
    }


    /*
     * 个人数据分析
     * */
    public function servicer_analyse(){

            if(Session::get('role_id') == 1 ) {
             $servicer = DB::table('servicer')
                 ->where('status',0)
                 ->select('id', 'name', 'face_url', 'telephone', 'nikename', 'worker_code')
                 ->get();
             $servicer = json_decode($servicer,1);

             foreach ($servicer as $key=>$value){
                 $servicer[$key]['face_url'] = '/face_url/'.$value['face_url'];
             }

         }else if(Session::get('role_id') == 2 ) {
             $servicer = DB::table('servicer')
                 ->where('company_id',Session::get('session_id'))
                 ->where('status',0)
                 ->select('id', 'name', 'face_url', 'telephone', 'nikename', 'worker_code')
                 ->get();

             $servicer = json_decode($servicer,1);

             foreach ($servicer as $key=>$value){
                 $servicer[$key]['face_url'] = '/face_url/'.$value['face_url'];
             }


         }
         return view('admin/analyse/servicer_analyse',['servicer'=>$servicer]);

    }

    /*
     * 盈利
     * */

    public function profit(Request $request){
        if(isset($request->servicer_id)){

           $order_servicer_percentage = DB::table('order_servicer_percentage')
               ->where('servicer_id',$request->servicer_id)
               ->get();

           return view('admin/analyse/profit',['order_servicer_percentage'=>$order_servicer_percentage]);


        }else if(isset($request->driver_id)){

        }
    }


    /*
     * 订单
     * */

     public function order(Request $request){
        if(isset($request->servicer_id)){
            $order_servicer_percentage = DB::table('order_servicer_percentage')
               ->where('servicer_id',$request->servicer_id)
               ->get();
            $order_id = [];
            foreach ($order_servicer_percentage as $key=>$value){
                $order_id[] = $value->order_id;
            }

            $order = DB::table('order')->whereIn('order_id',$order_id)->get();
            return view('admin/analyse/order',['order'=>$order]);

        }else if(isset($request->driver_id)){

        }
     }



}